using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Ccr.Core;
using Microsoft.Dss.Core.Attributes;
using Microsoft.Dss.ServiceModel.Dssp;
using Microsoft.Dss.ServiceModel.DsspServiceBase;
using W3C.Soap;
using submgr = Microsoft.Dss.Services.SubscriptionManager;
using touchsensor = Microsoft.Robotics.Services.Sample.Lego.Nxt.TouchSensor.Proxy;
using lightsensor = Microsoft.Robotics.Services.Sample.Lego.Nxt.LightSensor.Proxy;

namespace Lab1_206_10
{
    [Contract(Contract.Identifier)]
    [DisplayName("Lab1_206_10")]
    [Description("Lab1_206_10 service (no description provided)")]
    class Lab1_206_10Service : DsspServiceBase
    {
        /// <summary>
        /// Service state
        /// </summary>
        [ServiceState]
        Lab1_206_10State _state = new Lab1_206_10State();

        /// <summary>
        /// Main service port
        /// </summary>
        [ServicePort("/Lab1_206_10", AllowMultipleInstances = true)]
        Lab1_206_10Operations _mainPort = new Lab1_206_10Operations();

        [SubscriptionManagerPartner]
        submgr.SubscriptionManagerPort _submgrPort = new submgr.SubscriptionManagerPort();

        /// <summary>
        /// NxtTouchSensor partner
        /// </summary>
        [Partner("NxtTouchSensor", Contract = touchsensor.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UseExisting)]
        touchsensor.TouchSensorOperations _nxtTouchSensorPort = new touchsensor.TouchSensorOperations();
        touchsensor.TouchSensorOperations _nxtTouchSensorNotify = new touchsensor.TouchSensorOperations();

        /// <summary>
        /// NxtLightSensor partner
        /// </summary>
        [Partner("NxtLightSensor", Contract = lightsensor.Contract.Identifier, CreationPolicy = PartnerCreationPolicy.UseExisting)]
        lightsensor.LightSensorOperations _nxtLightSensorPort = new lightsensor.LightSensorOperations();
        lightsensor.LightSensorOperations _nxtLightSensorNotify = new lightsensor.LightSensorOperations();

        double lightintensity = 0;
        string colour=" ";

        /// <summary>
        /// Service constructor
        /// </summary>
        public Lab1_206_10Service(DsspServiceCreationPort creationPort)
            : base(creationPort)
        {
        }

        /// <summary>
        /// Service start
        /// </summary>
        protected override void Start()
        {

            // 
            // Add service specific initialization here
            // 

            base.Start();
            lightintensity = 0;
            SuscribeToTouchSensor();
            SuscribeToLightSensor();
        }
        void SuscribeToLightSensor()
        {
            lightsensor.LightSensorOperations lightsensorNotificationPort = new lightsensor.LightSensorOperations();
            _nxtLightSensorPort.Subscribe(lightsensorNotificationPort);
            Activate(
               Arbiter.Receive<lightsensor.Replace>
                   (true, lightsensorNotificationPort, lightSensorHandler));
        }
        void SuscribeToTouchSensor()
        {
            touchsensor.TouchSensorOperations touchsensorNotificationPort = new touchsensor.TouchSensorOperations();
            _nxtTouchSensorPort.Subscribe(touchsensorNotificationPort);
            Activate(
               Arbiter.Receive<touchsensor.TouchSensorUpdate>
                   (true, touchsensorNotificationPort, touchSensorHandler));

        }

        private void lightSensorHandler(lightsensor.Replace notification)
        {
            _nxtLightSensorPort.Spotlight(true);
            lightintensity = Convert.ToDouble(notification.Body.Intensity);          
        }

        private void touchSensorHandler(touchsensor.TouchSensorUpdate notification)
        {
            if (notification.Body.TouchSensorOn)
            {
                if (lightintensity >= 61)
                    colour = "White";
                else if (lightintensity >= 56 && lightintensity <= 60)
                    colour = "Light Light Grey";
                else if (lightintensity >= 51 && lightintensity <= 54)
                    colour = "Light Grey";
                else if (lightintensity >= 45 && lightintensity <= 48)
                    colour = "Grey";
                else if (lightintensity >= 41 && lightintensity <= 44)
                    colour = "Dark Grey";
                else
                    colour = "Black";
                Console.WriteLine("The light intensity is " + lightintensity);
                Console.WriteLine(colour);
            }
            
        }
        /// <summary>
        /// Handles Subscribe messages
        /// </summary>
        /// <param name="subscribe">the subscribe request</param>
        [ServiceHandler]
        public void SubscribeHandler(Subscribe subscribe)
        {
            SubscribeHelper(_submgrPort, subscribe.Body, subscribe.ResponsePort);
        }
    }
}